﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCS.DAL;
using SCS.Common.Models;
using System.Linq.Expressions;
namespace SCS.Business
{
    public class LearnerServices
    {
        IUnitOfWork _unitOfWork;
        public LearnerServices()
        {
            _unitOfWork = new UnitOfWork();
        }

        /// <summary>
        /// CountLearner
        /// </summary>
        /// <param name="search">string</param>
        /// <returns>quantity of learner</returns>
        public int CountLearner(string search)
        {
            if (string.IsNullOrEmpty(search))
            {
                return _unitOfWork.Learner.GetCount();
            }
            else
            {
                return _unitOfWork.Learner.GetSearchCount(search);
            }
        }

        /// <summary>
        /// CountLearner
        /// </summary>
        /// <param name="search">string</param>
        /// <returns>quantity of learner</returns>
        public IEnumerable<ClassInSchoolYear> GetClassSelectList()
        {
            int maxSchoolYear = _unitOfWork.SchoolYear.GetAll().Select(m=>m.schoolYearID).Max();
            return _unitOfWork.ClassInSchoolYear.Get(m => m.schoolYearID == maxSchoolYear);
        }

        /// <summary>
        /// GetLearnerPaging
        /// </summary>
        /// <param name="pageNumber">pageNumber</param>
        /// <param name="pageSize">pageSize</param>
        /// <returns>learner list</returns>
        public IEnumerable<Learner> GetLearnerPaging(int pageNumber, int pageSize)
        {
            return _unitOfWork.Learner.GetAll().Where(m => m.deactive == false);
        }

        /// <summary>
        /// GetLearner
        /// </summary>
        /// <param name="learnerID">learnerID</param>
        /// <returns>learner</returns>
        public Learner GetLearner(int learnerID)
        {
            return _unitOfWork.Learner.Get(m => m.learnerID == learnerID).FirstOrDefault();
        }

        /// <summary>
        /// GetAllParent
        /// </summary>
        /// <returns>Parent list</returns>
        public IEnumerable<Parent> GetAllParent()
        {
            return _unitOfWork.ParentViewTeacher.GetAll();
        }

        /// <summary>
        /// GetAllClassInSchoolYears
        /// </summary>
        /// <returns>class list</returns>
        public IEnumerable<ClassInSchoolYear> GetAllClassInSchoolYears()
        {
            int maxSchoolYear = _unitOfWork.SchoolYear.GetAll().Select(m => m.schoolYearID).Max();
            return _unitOfWork.ClassInSchoolYear.Get(m => m.schoolYearID == maxSchoolYear);
        }

        /// <summary>
        /// SearchTeacherPaging
        /// </summary>
        /// <param name="pageNumber">pageNumber</param>
        /// <param name="pageSize">pageSize</param>
        /// <param name="search">string</param>
        /// <returns>learner list</returns>
        public IEnumerable<Learner> SearchTeacherPaging(int pageNumber, int pageSize, string search, int ClassID)
        {
            return _unitOfWork.Learner.SearchLearnerPaging(ClassID, search);
        }

        /// <summary>
        /// CreateLearner
        /// </summary>
        /// <param name="learner">learner</param>
        /// <returns>bool</returns>
        public bool CreateLearner(Learner learner)
        {
            try
            {
                int id = _unitOfWork.Learner.GetAll().Max(t => (int?)t.learnerID) ?? 0;
                learner.learnerID = id + 1;
                _unitOfWork.Learner.Add(learner);
                _unitOfWork.Commit();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        /// <summary>
        /// UpdateLearner
        /// </summary>
        /// <param name="learner">Learner</param>
        /// <returns>bool</returns>
        public bool UpdateLearner(Learner learner)
        {
            try
            {
                _unitOfWork.Learner.Update(learner);
                _unitOfWork.Commit();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        /// <summary>
        /// UpdateLearnerDK
        /// </summary>
        /// <param name="learner">Learner</param>
        /// <returns>bool</returns>
        public bool UpdateLearnerDK(Learner learner)
        {
            try
            {
                //int id = _unitOfWork.Parent2.GetAll().Max(t => (int?)t.parentID) ?? 0;
                //learner.parentID = id;
                _unitOfWork.Learner.Update(learner);
                _unitOfWork.Commit();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        /// <summary>
        /// AddParentIDToLearner
        /// </summary>
        /// <param name="learnerID">learnerID</param>
        /// <param name="parentID">parentID</param>
        /// <returns></returns>
        public bool AddParentIDToLearner(int learnerID, int parentID)
        {
            try
            {
                Learner learner = new Learner();
                learner = _unitOfWork.Learner.Get(m => m.learnerID == learnerID).FirstOrDefault();
                learner.parentID = parentID;
                _unitOfWork.Learner.Update(learner);
                _unitOfWork.Commit();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        /// <summary>
        /// DeleteLearner
        /// </summary>
        /// <param name="ID">ID</param>
        /// <returns>bool</returns>
        public bool DeleteLearner(int ID)
        {
            try
            {
                Learner deleteLearner = new Learner();
                deleteLearner = GetLearner(ID);
                _unitOfWork.Learner.Delete(deleteLearner);
                _unitOfWork.Commit();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }
}